<script setup lang="ts">
// 图表例子
import { onMounted, onUnmounted, ref } from 'vue'
import type { Dayjs } from 'dayjs'
import LineEcharts from './LineEcharts.vue'
import { useGlobalStore } from '@/store'
import BarEcharts from '@/components/BarEcharts.vue'
import { throttle } from 'lodash-es'

type RangeValue = [Dayjs, Dayjs]

const globalStore = useGlobalStore()
const inputVal = ref<RangeValue>()

const handleSearch = () => {
	// 将输入的搜索条件传递给全局搜索参数
	globalStore.setFilterParams({
		params1: inputVal.value
	})
}

// 处理页面滚动事件
const handleScroll = () => {
	// 在滚动事件中，循环判断页面中这个图表是否在可视区内
	const echartsIdList = ['line-chart1', 'bar-chart1', 'line-chart2', 'bar-chart2', 'line-chart3', 'bar-chart3']
	const isInViewList = echartsIdList.filter((item) => {
		const childRect = document.getElementById(item).getBoundingClientRect()
		const parentRect = document.getElementById('analysis_container').getBoundingClientRect()

		// 子元素的顶部在父元素的底部之上，且子元素的底部在父元素的顶部之下，则认为子元素在可视区内
		return childRect.top < parentRect.bottom && childRect.bottom > parentRect.top
	})

	globalStore.setIsInViewList(isInViewList ?? [])
}

/**
 * 1、进入此页面开始监听页面滚动
 * 2、每次页面滚动时，触发页面的滚动事件
 * 3、在滚动事件中，循环判断页面中这个图表是否在可视区内
 * 4、如果在可视区内，则判断图表是否有数据，如果没有数据，则重新请求数据渲染图表
 * */
onMounted(() => {
	// 页面初始化先判断哪些 id 在可视区内
	handleScroll()

	// 进入此页面开始监听页面滚动
	const scrollWrapper = document.getElementById('analysis_container')
	const onScroll = throttle(handleScroll, 300)
	scrollWrapper?.removeEventListener('scroll', onScroll)
	scrollWrapper.addEventListener('scroll', onScroll, true)
})

onUnmounted(() => {
	// 离开页面时，移除页面滚动事件监听
	const scrollWrapper = document.getElementById('analysis_container')
	const onScroll = throttle(handleScroll, 500)
	scrollWrapper?.removeEventListener('scroll', onScroll)
})
</script>

<template>
	<div class="echarts-demo-container">
		<!--1、筛选条件-->
		<div class="search-bar-wrapper">
			<a-input v-model:value="inputVal" placeholder="请输入搜索内容" />
			<a-button type="primary" @click="handleSearch">查询</a-button>
		</div>
		<div class="analysis-container" id="analysis_container">
			<!--2、折线图、柱状图组件-->
			<div class="item-echarts">
				<LineEcharts title="折线图1" id="line-chart1" />
			</div>
			<div class="item-echarts">
				<BarEcharts title="柱状图1" id="bar-chart1" />
			</div>

			<div class="item-echarts">
				<LineEcharts title="折线图2" id="line-chart2" />
			</div>
			<div class="item-echarts">
				<BarEcharts title="柱状图2" id="bar-chart2" />
			</div>

			<div class="item-echarts">
				<LineEcharts title="折线图3" id="line-chart3" />
			</div>
			<div class="item-echarts">
				<BarEcharts title="柱状图3" id="bar-chart3" />
			</div>
		</div>
	</div>
</template>

<style scoped lang="less">
.echarts-demo-container {
	height: calc(100vh - 200px);
	overflow-y: hidden;

	.search-bar-wrapper {
		width: 1000px;
		display: flex;
		justify-content: space-between;
		align-items: center;
		border: 1px solid #ccc;
		border-radius: 8px;
		padding: 10px;
	}

	.analysis-container {
		// 注意：一定要固定高度，否则 echarts 不是在 analysis-container 中滚动，而是页面整体滚动
		width: 1000px;
		height: calc(100vh - 250px);
		overflow-y: auto;
	}

	.item-echarts {
		width: calc(800px + 20px);
		height: calc(500px + 20px);
		padding: 10px;
		border: 1px solid #ccc;
		border-radius: 8px;
		margin: 20px 0;
	}
}
</style>
